#include "form_client.h"
#include "ui_form_client.h"
#include "login.h"
#include<QMovie>
#include <QToolButton>
#include <QMessageBox>

Form_client::Form_client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_client)
{
    ui->setupUi(this);
    //给lab设置动画
    QMovie *movie1 = new QMovie(this);
    movie1->setFileName(":/C:/client_logo/backgrd.gif");
    movie1->start();
    ui->backgrd->setMovie(movie1);

    ui->ip->hide();
    ui->port->hide();

}

Form_client::~Form_client()
{
    delete ui;
}

void Form_client::receiveridData(QString iddata)
{
    ui->myID->setText(iddata);
    QString username1 = ui->myID->text();
    if(username1 == "贞德食泥鸭")
    {
        //加载好友

        QToolButton *btn1 = new QToolButton(this);
        //加载图片
        btn1->setIcon(QPixmap(":/C:/client_logo/girl1.jpg"));
        //设置图片大小
        btn1->setIconSize(QSize(100,100));
        //设置网名
        btn1->setText(QString("AAA水果批发"));
        //设置为透明
        btn1->setAutoRaise(true);
        //设置显示格式
        btn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn1);

        QToolButton *btn2 = new QToolButton(this);
        //加载图片
        btn2->setIcon(QPixmap(":/C:/client_logo/boy1.jpg"));
        //设置图片大小
        btn2->setIconSize(QSize(100,100));
        //设置网名
        btn2->setText(QString("孤独的狼"));
        //设置为透明
        btn2->setAutoRaise(true);
        //设置显示格式
        btn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn2);

        //加载聊天室
        QToolButton *btn_1 = new QToolButton(this);
        //加载图片
        btn_1->setIcon(QPixmap(":/C:/client_logo/chat1.png"));
        //设置图片大小
        btn_1->setIconSize(QSize(100,100));
        //设置网名
        btn_1->setText(QString("交流群1"));
        //设置为透明
        btn_1->setAutoRaise(true);
        //设置显示格式
        btn_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlaout2->addWidget(btn_1);

        //好友
        QList<QToolButton *>vector;
        vector.push_back(btn1);
        vector.push_back(btn2);
        isshow.push_back(false);
        isshow.push_back(false);
        //聊天室
        QList<QToolButton *>vector2;
        vector2.push_back(btn_1);
        isshow2.push_back(false);

        //好友
        for(int i=0;i<vector.length();i++)
        {
            connect(vector[i],&QToolButton::clicked,[=](){
                if(isshow[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow[i] = true;
                //实例化mainchat
                this->chat = new mainchat(nullptr,vector[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector[i]->icon());
                chat->setWindowTitle(vector[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());

                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow[i]=false;
                });
            });
        }

        //聊天室
        for(int i=0;i<vector2.length();i++)
        {
            connect(vector2[i],&QToolButton::clicked,[=](){
                if(isshow2[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow2[i] = true;
                //实例化mainchat
                this->chat = new mainchat(nullptr,vector2[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector2[i]->icon());
                chat->setWindowTitle(vector2[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());

                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow2[i]=false;
                });
            });
        }

    }
    if(username1 == "孤独的狼")
    {
        QToolButton *btn1 = new QToolButton(this);
        //加载图片
        btn1->setIcon(QPixmap(":/C:/client_logo/girl1.jpg"));
        //设置图片大小
        btn1->setIconSize(QSize(100,100));
        //设置网名
        btn1->setText(QString("AAA水果批发"));
        //设置为透明
        btn1->setAutoRaise(true);
        //设置显示格式
        btn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn1);

        QToolButton *btn2 = new QToolButton(this);
        //加载图片
        btn2->setIcon(QPixmap(":/C:/client_logo/boy1.jpg"));
        //设置图片大小
        btn2->setIconSize(QSize(100,100));
        //设置网名
        btn2->setText(QString("贞德食泥鸭"));
        //设置为透明
        btn2->setAutoRaise(true);
        //设置显示格式
        btn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn2);

        //加载聊天室
        QToolButton *btn_1 = new QToolButton(this);
        //加载图片
        btn_1->setIcon(QPixmap(":/C:/client_logo/chat1.png"));
        //设置图片大小
        btn_1->setIconSize(QSize(100,100));
        //设置网名
        btn_1->setText(QString("交流群1"));
        //设置为透明
        btn_1->setAutoRaise(true);
        //设置显示格式
        btn_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlaout2->addWidget(btn_1);

        QList<QToolButton *>vector;
        vector.push_back(btn1);
        vector.push_back(btn2);
        isshow.push_back(false);
        isshow.push_back(false);

        //聊天室
        QList<QToolButton *>vector2;
        vector2.push_back(btn_1);
        isshow2.push_back(false);

        for(int i=0;i<vector.length();i++)
        {
            connect(vector[i],&QToolButton::clicked,[=](){
                if(isshow[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow[i] = true;

                this->chat = new mainchat(nullptr,vector[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector[i]->icon());
                chat->setWindowTitle(vector[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());


                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow[i]=false;
                });
            });
        }

        //聊天室
        for(int i=0;i<vector2.length();i++)
        {
            connect(vector2[i],&QToolButton::clicked,[=](){
                if(isshow2[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow2[i] = true;
                //实例化mainchat
                this->chat = new mainchat(nullptr,vector2[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector2[i]->icon());
                chat->setWindowTitle(vector2[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());

                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow2[i]=false;
                });
            });
        }
    }

    if(username1 == "AAA水果批发")
    {
        QToolButton *btn1 = new QToolButton(this);
        //加载图片
        btn1->setIcon(QPixmap(":/C:/client_logo/boy1.jpg"));
        //设置图片大小
        btn1->setIconSize(QSize(100,100));
        //设置网名
        btn1->setText(QString("孤独的狼"));
        //设置为透明
        btn1->setAutoRaise(true);
        //设置显示格式
        btn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn1);

        QToolButton *btn2 = new QToolButton(this);
        //加载图片
        btn2->setIcon(QPixmap(":/C:/client_logo/boy1.jpg"));
        //设置图片大小
        btn2->setIconSize(QSize(100,100));
        //设置网名
        btn2->setText(QString("贞德食泥鸭"));
        //设置为透明
        btn2->setAutoRaise(true);
        //设置显示格式
        btn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlayout->addWidget(btn2);

        //加载聊天室
        QToolButton *btn_1 = new QToolButton(this);
        //加载图片
        btn_1->setIcon(QPixmap(":/C:/client_logo/chat1.png"));
        //设置图片大小
        btn_1->setIconSize(QSize(100,100));
        //设置网名
        btn_1->setText(QString("交流群1"));
        //设置为透明
        btn_1->setAutoRaise(true);
        //设置显示格式
        btn_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //放到vlayout布局中
        ui->vlaout2->addWidget(btn_1);

        QList<QToolButton *>vector;
        vector.push_back(btn1);
        vector.push_back(btn2);
        isshow.push_back(false);
        isshow.push_back(false);
        //聊天室
        QList<QToolButton *>vector2;
        vector2.push_back(btn_1);
        isshow2.push_back(false);

        for(int i=0;i<vector.length();i++)
        {
            connect(vector[i],&QToolButton::clicked,[=](){
                if(isshow[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow[i] = true;

                this->chat = new mainchat(nullptr,vector[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector[i]->icon());
                chat->setWindowTitle(vector[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());


                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow[i]=false;
                });
            });
        }
        //聊天室
        for(int i=0;i<vector2.length();i++)
        {
            connect(vector2[i],&QToolButton::clicked,[=](){
                if(isshow2[i])
                {
                    QMessageBox::warning(this,"警告","该聊天窗已打开!");
                    return;
                }
                isshow2[i] = true;
                //实例化mainchat
                this->chat = new mainchat(nullptr,vector2[i]->text());
                connect(this,SIGNAL(sendipData(QString)),chat,SLOT(getipData(QString)));
                connect(this,SIGNAL(sendportData(QString)),chat,SLOT(getportData(QString)));
                connect(this,SIGNAL(sendmyname(QString)),chat,SLOT(getmyname(QString)));

                chat->setWindowIcon(vector2[i]->icon());
                chat->setWindowTitle(vector2[i]->text());
                chat->show();

                emit sendipData(ui->ip->text());
                emit sendportData(ui->port->text());
                emit sendmyname(ui->myID->text());

                //关闭事件对应的isshow变为false
                connect(chat,&mainchat::closechat,this,[=](){
                    isshow2[i]=false;
                });
            });
        }

    }
}

void Form_client::getipData(QString data)
{
    ui->ip->setText(data);
}

void Form_client::getportData(QString data)
{
    ui->port->setText(data);
}

void Form_client::on_close_clicked()
{
    this->close();
}

void Form_client::on_min_clicked()
{
    this->showMinimized();
}
